package com.circle.model.module.log;



import com.circle.model.module.StrBuffer;
import com.circle.model.util.DateUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import javax.swing.JOptionPane;

/**
 * 责任链模式
 */
public abstract class LoggerABC {

    public enum LogLevel {
        DEBUG,
        INFO,
        WARN,
        ERROR
    }

    public static final int INFO = 0;
    public static final int DEBUG = 1;
    public static final int WARN = 2;
    public static final int ERROR = 3;

    protected int level;
    private File logDir;
    //责任链中的下一个元素
    protected LoggerABC nextLoggerABC;
    protected Exception exception;

    public void setNextLoggerABC(LoggerABC nextLoggerABC) {
        this.nextLoggerABC = nextLoggerABC;
    }

    public void setException(Exception exception) {
        this.exception = exception;
    }

    public LoggerABC() {
        try {
            // 获取程序运行目录
            String baseDir = System.getProperty("user.dir");

            // 创建日志目录
            File mdNotesLogDir = new File(baseDir, "DOCS_Log");
            if (!mdNotesLogDir.exists()) {
                if (!mdNotesLogDir.mkdirs()) {
                    JOptionPane.showMessageDialog(null, 
                        "创建日志目录失败", 
                        "错误", 
                        JOptionPane.ERROR_MESSAGE);
                    return;
                }
            }
            this.logDir = mdNotesLogDir;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, 
                "创建日志目录失败: " + e.getMessage(), 
                "错误", 
                JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
    }

    public void logMessage(int level, String message) {
        if (this.level == level) {
            write(message);
        }
        if (nextLoggerABC != null) {
            nextLoggerABC.logMessage(level, message);
        }
    }

    /**
     * 向txt文件末行追加内容
     *
     * @param filename 文件名称
     * @param content  要追加的内容
     */
    protected void appendToFile(String filename, String content) {
        try {
            StrBuffer sbuffer = new StrBuffer();
            sbuffer.append(filename);
            sbuffer.append("_");
            sbuffer.append(DateUtils.formatDate());
            sbuffer.append(".txt");
            String child = sbuffer.toString();
            File fileLog = new File(logDir, child);
            // 使用try-with-resources语句自动关闭流资源
            try (FileOutputStream fileOutputStream = new FileOutputStream(fileLog, true);
                 // 使用OutputStreamWriter指定编码格式为UTF-8
                 OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
                 // 使用PrintWriter进行格式化输出
                 PrintWriter printWriter = new PrintWriter(outputStreamWriter)) {

                // 写入内容
                printWriter.println("***********************************************");
                printWriter.println(DateUtils.getCurrentDateSSS());
                printWriter.println(content);
                printWriter.println("");

                // 显式刷新缓冲区，确保所有数据都被写入文件
                printWriter.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("写入文件时发生错误：" + e.getMessage());
        }
    }

    protected abstract void write(String message);
}
